package com.ca.ridersclubapplication;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ca.ridersclubapplication.adapter.AccountAdapter;
import com.ca.ridersclubapplication.adapter.PaperHistoryAdapter;
import com.ca.ridersclubapplication.data.DataHandler;
import com.ca.ridersclubapplication.request.AfterExecutor;
import com.ca.ridersclubapplication.request.PaperRequester;
import com.ca.ridersclubapplication.request.dto.HistoryDto;
import com.ca.ridersclubapplication.request.dto.Result;
import com.ca.ridersclubapplication.view.CallbackView;

import java.util.List;

public class HistoryListActivity extends ImmersiveActivity {

    private ImageView backView;
    private TextView titleView;
    private RecyclerView rv;
    private PaperHistoryAdapter adapter;

    private ProgressBar progressBar;
    private Long startId = Long.MAX_VALUE;
    private boolean canRequest = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_list);

        initView();

        initListener();

        loadData();
    }

    private void initView() {

        backView = findViewById(R.id.iv_back);
        titleView = findViewById(R.id.tv_title);

        rv = findViewById(R.id.rv);
        adapter = new PaperHistoryAdapter(null);
        adapter.setActivity(this);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);

        progressBar = findViewById(R.id.bar_progress);

        //  历史记录会因用户查看的文章而发生变化，因此放入数据处理器中
        DataHandler.putView("AddHistory", new CallbackView() {
            @Override
            public void update() {
                //  请求历史记录
                listHistory(true);
            }
        });
    }

    private void initListener() {

        backView.setOnClickListener(v -> {
            finishAfterTransition();
        });
    }

    private void loadData() {

        listHistory(false);
    }

    private synchronized void listHistory(boolean all) {
        if (canRequest) {
            canRequest = false;
            progressBar.setVisibility(View.VISIBLE);
            if (all) {
                startId = Long.MAX_VALUE;
            }
            PaperRequester.listHistory(this, startId, new AfterExecutor<List<HistoryDto>>() {
                @Override
                public void execute(Result<List<HistoryDto>> result) {
                    List<HistoryDto> data = result.getData();
                    if (result.isSuccess() && data != null) {

                        if (all) {
                            adapter.appendList(data);
                        }
                        else {
                            adapter.setList(data);
                        }

                        if (data.size() > 0) {
                            startId = data.get(data.size() - 1).getId();
                        }
                    }

                    progressBar.setVisibility(View.GONE);
                    canRequest = true;
                }
            });
        }
        else {
            progressBar.setVisibility(View.GONE);
        }
    }
}